import 'package:flutter/material.dart';
import 'package:flutter_hsh/fluttergromore/ads/ads_utils.dart';
import 'package:get/get.dart';

class MyRouteObserver extends GetObserver {
  // 新页面被推入时调用
  @override
  void didPush(Route route, Route? previousRoute) {
    super.didPush(route, previousRoute);
    // 获取当前路由名称（GetPage的name）
    final currentRoute = route.settings.name;
    // 获取上一个路由名称
    final previousRouteName = previousRoute?.settings.name;
    AdsUtils.showInterstitialAd();
    debugPrint('MyRouteObserver 路由进入：$currentRoute，来自：$previousRouteName');
    // 可在这里添加日志记录、统计等逻辑
  }

  // 页面被弹出时调用（返回上一页）
  @override
  void didPop(Route route, Route? previousRoute) {
    super.didPop(route, previousRoute);
    final poppedRoute = route.settings.name; // 被弹出的路由
    final currentRoute = previousRoute?.settings.name; // 回到的路由

    debugPrint('MyRouteObserver 路由离开：$poppedRoute，回到：$currentRoute');
  }

  // 路由被替换时调用（如Get.offNamed）
  @override
  void didReplace({Route? newRoute, Route? oldRoute}) {
    super.didReplace(newRoute: newRoute, oldRoute: oldRoute);
    final newRouteName = newRoute?.settings.name;
    final oldRouteName = oldRoute?.settings.name;

    debugPrint('MyRouteObserver 路由替换：$oldRouteName -> $newRouteName');
  }

  // 手势返回开始（如iOS侧滑返回开始）
  @override
  void didStartUserGesture(Route route, Route? previousRoute) {
    super.didStartUserGesture(route, previousRoute);
    debugPrint('MyRouteObserver 开始手势返回：${route.settings.name}');
  }

  // 手势返回结束
  @override
  void didStopUserGesture() {
    super.didStopUserGesture();
    debugPrint('MyRouteObserver 结束手势返回');
  }
}